Ext.define('wmf.controller.blacklist.blacklist', {
    extend: 'Ext.app.Controller',
    stores: ['blacklist.blacklist',
             'blacklist.tree',
             'blacklist.blacklistGrid',
			 'blacklist.query'],
    models: ['blacklist.blacklist'],
    views: ['blacklist.main', 
            'blacklist.List', 
            'blacklist.Edit',
            'blacklist.query',
            'blacklist.east',
			'blacklist.queryData'
            ],
    refs: [{
        ref: 'blacklistList',
        selector: 'blacklistList'
    },{
        ref: 'blacklistEast',
        selector: 'blacklistEast'
	},{
        ref: 'blacklistMain',
        selector: 'blacklistMain'
    },{
        ref: 'blacklistEdit',
        selector: 'blacklistEdit'
    },{
    	ref: 'blacklistQuery',
    	selector: 'blacklistQuery'
    },{
    	ref: 'blacklistQueryData',
    	selector: 'blacklistQueryData'
    }],
    
    init: function () {
        this.control({
        	'blacklistList' : {
			    beforerender : this.onBeforeRender
			},
			'blacklistEast tool#minimize': {
				click: this.onMinToolClick
			},
			'blacklistQuery button#query': {//'查询页面'下的 '查询'
				click: this.onQueryQueryButtonClick
			},
			'blacklistQuery button#cancel': {//'查询页面'下的 '取消'
				click: this.onMinToolClick
			},
			'blacklistEdit button#cancel': {//'编辑页面'下的 '取消'
				click: this.onMinToolClick
			},
			'blacklistEdit button#submitMobile': {//'编辑页面'下的 '提交号码' 
				click: this.onEditSubmitMobileClick
			},
			'blacklistEdit button#submitFile': {//'编辑页面'下的 '提交文件'
				click: this.onEditSubmitFileClick
			},
			'blacklistList button#query': {//黑名单页面的'查询'
                click: this.onQueryButtonClick
            },
			'blacklistList button#edit': {//黑名单页面的'编辑'
                click: this.onEditButtonClick
            }
        });
        wmf.blacklist = {};
        wmf.blacklist.params = {};
        wmf.blacklist.data = {};
        wmf.blacklist.init = true;
    },

	//点击黑名单页面的'查询'按钮操作
    onQueryButtonClick: function(){
        var win = this.getBlacklistEast();
        win.removeAll();
        win.add(Ext.widget("blacklistQuery"));
        win.width = "40%";
        win.hide();
        win.show();
        wmf.blacklist.params = {};
        this.getBlacklistQueryData().getStore().loadData([]);
    },
	
	//点击黑名单页面的'编辑'按钮操作
	onEditButtonClick: function(){
        var win = this.getBlacklistEast();
        win.removeAll();
        win.add(Ext.widget("blacklistEdit"));
		win.width = "40%";
        win.hide();
        win.show();
    },
	
	//点击 '查询页面' 下的 '查询' 按钮操作
	onQueryQueryButtonClick: function(){
    	var me = this;
    	var former = this.getBlacklistQuery();
    	var value = former.getValues();
		wmf.blacklist.init = true;
		wmf.blacklist.params.mobile = value.mobile;
		me.getBlacklistList().getStore().pagebar.getStore().currentPage = 1;
		me.getBlacklistQueryData().getStore().reload();
    },
	
	//点击 '编辑页面' 下的 '保存' 按钮操
	onEditSaveButtonClick: function(){
    	var me = this;
    	var win = me.getBlacklistEast();
    	var former = me.getBlacklistEdit();
    	var value = former.getValues();
    	Ext.Ajax.request({
			url:'blacklist/save.do',
			params: value,
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getBlacklistList().getStore().reload();
			},
			failure : function(respond, action) {
				Ext.Msg.alert("提示", '网络错误');
			}
		});
    },
	
	//点击 '编辑页面' 下的 '提交号码' 按钮操
	onEditSubmitMobileClick: function(){
		var me = this;
    	var win = me.getBlacklistEast();
    	var former = me.getBlacklistEdit();
    	var value = former.getValues();
    	if(!value.blackItem){
    		Ext.Msg.alert('提示', '请输入号码');
    		return false;
    	}
    	Ext.Ajax.request({
			url:'blacklist/saveBlacklist.do',
			params: value,
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getBlacklistList().getStore().reload();
			},
			failure : function(respond, action) {
	            Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
	},
	
	//点击 '编辑页面' 下的 '提交文件' 按钮
	onEditSubmitFileClick: function(){
		var me = this;
    	var win = me.getBlacklistEast();
    	var former = me.getBlacklistEdit();
    	former.getForm().submit({
			url:'blacklist/batchSaveBlacklist.do',
			method : 'POST',
			waitMsg: '上传中...',
			success : function(respond, opts) {
				var responds = opts.result;
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getBlacklistList().getStore().reload();
			},
			failure : function(respond, action) {
	            Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
	},
	
	onMinToolClick: function(){
    	var win = this.getBlacklistEast();
    	win.hide();
    },
    
    onBeforeRender: function(component,options){
    	wmf.data.loadAction(this.getBlacklistMain().itemId, this.getBlacklistList());
    	wmf.blacklist.params = {};
    	component.getStore().load();
    }
});
